/******************************************************************************
* SVN­Version­Information:
* ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
* $LastChangedRevision: 6 $
* $LastChangedBy: c.a.duevel $
* $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
* $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJavaFX/src/de/hft_stuttgart/robofight/client/javafx/Login.fx $
******************************************************************************/

package de.hft_stuttgart.robofight.client.javafx;


import de.hft_stuttgart.botwar.common.interfaces.IRemotePlayer;
import de.hft_stuttgart.robofight.client.java.JLogin;
import de.hft_stuttgart.robofight.client.java.JPlayer;
import de.hft_stuttgart.robofight.client.javafx.util.*;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.control.TextBox;
import javafx.scene.control.Button;
import javafx.scene.Cursor;
import javafx.scene.shape.Rectangle;
import javafx.scene.input.MouseEvent;

/**
 * @author ralf.dauenhauer@gmx.de, michael.rieker@web.de
 */

public class Login extends CustomNode {

    var fScreen = false;
    var jLogin : JLogin;
    var local : String = " ";

    package var getParam : function() : String;
    package var getPlayer : function() : JPlayer;
    package var getNodeByName : function(nodeName : String) : Node;
    package var switchNode : function(className : String) : Void;
    package var createPlayer : function(player: IRemotePlayer) : Boolean;
    var player : JPlayer;

    var username = TextBox {
        var fxzLayout = getNodeByName("loginRahmen_user").layoutBounds;
        font : Constants.FONT_TEXTFIELD
        layoutX: fxzLayout.minX + 7,
        layoutY: fxzLayout.minY + 7,
        width: fxzLayout.maxX - fxzLayout.minX -15;
        height : 20;
        skin: Constants.getTextBoxSkin()
    }

    var passwordField : SwingPasswordField = SwingPasswordField{
        var fxzLayout = getNodeByName("loginRahmen_pw").layoutBounds;
        layoutX: fxzLayout.minX + 10,
        layoutY: fxzLayout.minY + 8,
        text: bind password with inverse
        action: function() {
             loginSwing = (password == systemPassword);
        }
        foreground: Color.WHITE
    }

    var loginButton : Button = Button {
         var fxzLayout = getNodeByName("loginButtonLogin").layoutBounds;
         layoutX : fxzLayout.minX
         layoutY : fxzLayout.minY
         text: "login"
         action: login
         font: Constants.FONT_BUTTON
         cursor : Cursor.HAND
         width: 150
         height : 30
         skin : Constants.getButtonSkin();
    }

    var loginSwing = false;
    var password = "";
    var systemPassword = "12345";

    var group : Group = Group {
            content: [
                getNodeByName("background"),
                getNodeByName("logo_big"),
                getNodeByName("loginRahmen"),
                getNodeByName("loginRahmen_user"),
                getNodeByName("loginRahmen_pw"),
                getNodeByName("loginPassword"),
                getNodeByName("loginUsername"),
                getNodeByName("loginRegister_pwVergessen"),
                username,
                passwordField,
                loginButton,
                Rectangle {
                    var fxzLayout = getNodeByName("loginRegister_pwVergessen").layoutBounds;
                    x : fxzLayout.minX;
                    y : fxzLayout.minY;
                    width : 65
                    height : 15
                    cursor : Cursor.HAND;
                    onMouseClicked : function( e : MouseEvent) : Void {
                        register();
                    }
                    opacity:0
                }
            ]
         };

    init {
        username.requestFocus();
    }
    
    override function create() : Node {
      return group;
   }


    function login(): Void{
        jLogin = new JLogin();
        jLogin.setLocal(getParam());
        var tmp : String;
        if((tmp = jLogin.connect()) == null){
            if(createPlayer(jLogin.login({username.text} as String,{password} as String))){
                switchNode("Lobby");
            } else{
                insert Message {
                        msg: jLogin.getException();
                        msgBoxBackground: getNodeByName("msgBoxmessageBox")

                        } into group.content;
            }
        } else {
                insert Message {
                        msg: tmp;
                        msgBoxBackground: getNodeByName("msgBoxmessageBox")

                        } into group.content;
        }
    }

    function register(){
        switchNode("Register");
    }
}
